// import router from '@/credit/router'
import { getData } from '@vimi/utils-api'

const getFirstData = res => {
  return (res && res.data && res.data.length && res.data[0]) || {}
}

const state = {
  taskCountGov: {},
}

const getters = {
  taskCountGov: state => state.taskCountGov,
}

const actions = {
  getTaskCountGov({ commit, rootGetters }) {
    return new Promise((resolve, reject) => {
      getData('credit', 'credit_my_task_list_gov_view', {
        'user_id.eq': rootGetters.userId,
      })
        .then(res => {
          const data = getFirstData(res)
          commit('taskCountGov', data)
          resolve(data)
          return data
        })
        .catch(err => {
          reject(err)
        })
    })
  },
}

const mutations = {
  taskCountGov: (state, payload) => (state.taskCountGov = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
